﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.AppCode;
using RibbonEditor.LocLabels;
using System;

namespace RibbonEditor._Global
{
    class LocLabels: IXmlBuildable, IChildDeletable
    {
         public List<LocLabel> LocalLabelsList;

        public LocLabels()
        {
            this.LocalLabelsList = new List<LocLabel>();
        }

        public LocLabels(XmlNode node)
            : this()
        {
            try
            {
                foreach (XmlNode cmdXmlNode in node.SelectNodes("LocLabel"))
                {
                    if (cmdXmlNode.NodeType == XmlNodeType.Comment)
                        return;

                    LocLabel label = new LocLabel(cmdXmlNode);
                    this.LocalLabelsList.Add(label);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonDiffXmlNode)
        {
            try
            {
                XmlNode locLabelsXmlNode = XmlHelper.AddNode(ribbonDiffXmlNode, "LocLabels");

                foreach (LocLabel label in this.LocalLabelsList)
                {
                    label.AddNode(locLabelsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode locLabelsNode = new TreeNode("LocLabels");
                locLabelsNode.ContextMenuStrip = this.BuildContextMenu(locLabelsNode);
                locLabelsNode.Tag = this;


                foreach (LocLabel label in this.LocalLabelsList)
                {
                    label.BuildTreeViewElement(locLabelsNode);
                }

                parentNode.Nodes.Add(locLabelsNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Localized label");
            tsmi.Click += new System.EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);

            return cms;
        }

        void tsmi_Click(object sender, System.EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                LocLabel label = new LocLabel();
                label.BuildTreeViewElement(selectedNode);

                selectedNode.Expand();

                this.LocalLabelsList.Add(label);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is LocLabel)
                {
                    this.LocalLabelsList.Remove((LocLabel)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
